﻿using System;
using System.Windows.Input;
using Mvvm.Common.Commands;

namespace Mvvm.Common
{

    public delegate void WindowClosedHandel();

    public class CloseModel
    {
        private readonly Func<bool> _closeFunc = null;
        public event WindowClosedHandel WindowClosed;
        /// <summary>
        /// 关闭命令
        ///<para>bb 2020-03-20 17:06:31</para>
        /// </summary>
        public ICommand CloseCommand { get; set; }

        public CloseModel(Func<bool> closeFunc=null)
        {
            _closeFunc = closeFunc;
            CloseCommand=new DelegateCommand(Close);
        }
        
        /// <summary>
        /// 关闭
        /// </summary>
        public void Close()
        {
            bool isClose = true;
            if (_closeFunc != null)
            {
                isClose = _closeFunc.Invoke();
            }

            if (!isClose) return;
            if (WindowClosed == null) return;
            WindowClosed?.Invoke();
        }
    }
}
